package com.evon.yardmanagement.ui.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.interceptor.validation.SkipValidation;

import com.evon.yardmanagement.service.UtilityService;
import com.evon.yardmanagement.service.VendorManagementService;
import com.evon.yardmanagement.ui.dto.CountryDTO;
import com.evon.yardmanagement.ui.dto.StateDTO;
import com.evon.yardmanagement.ui.dto.VendorDTO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

public class AddVendorAction extends ActionSupport implements Preparable{
	
	public AddVendorAction(){
		listCountry = new UtilityService().getCountries();

		// selectedCountryId = dto.getState().getCountry().getId();

		// set selected state
		listState = UtilityService.getState(selectedCountryId);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private VendorDTO dto;

	private List<CountryDTO> listCountry = new ArrayList<CountryDTO>();;

	private int selectedCountryId;

	private List<StateDTO> listState = new ArrayList<StateDTO>();

	@SkipValidation
	public String showStates() {
		listCountry = new UtilityService().getCountries();

		// selectedCountryId = dto.getState().getCountry().getId();

		// set selected state
		listState = UtilityService.getState(selectedCountryId);

		return SUCCESS;
	}
	@Validations(requiredStrings = {
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.name", message = "You must enter a character name."),
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.city", message = "You must enter a city name."),
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.phone", message = "You must enter a phone number."),
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.address", message = "You must enter address."),
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.zipCode", message = "You must enter zip code."),
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.email", message = "You must enter email id.") 
			},
			regexFields = {
			@RegexFieldValidator(fieldName="dto.phone", expression="^[0-9]+",message="Phone can only be numeric", type=ValidatorType.FIELD),
			@RegexFieldValidator(fieldName="dto.zipCode", expression="^[0-9]+",message="Zip code can only be numeric", type=ValidatorType.FIELD)
			}
	)
	@StringLengthFieldValidator(fieldName="dto.phone", message = "Phone number must be between 9 and 12 characters", 
                              minLength = "9", maxLength = "12")
    @EmailValidator(fieldName="dto.email",type= ValidatorType.FIELD, message="You must enter proper email id")
  
   
	public String addVendor() {
		// TODO: move this to global data
		listCountry = new UtilityService().getCountries();
		listState = UtilityService.getState(selectedCountryId);
		// set the state
		if(!validInput()){
			return "input";
		}
		
		boolean isUpdate = dto.getId() > 0;
		VendorManagementService.addVendor(dto);
		if (isUpdate) {
			addActionMessage("Successfully Updated vendor - "+dto.getName());
		} else {
			addActionMessage("Vendor " + dto.getName()+" added successfully");
		}
		return "redirect";
	}

	public VendorDTO getDto() {
		return dto;
	}
	
	private boolean validInput() {
		if(selectedCountryId <= 0){
			addActionMessage("Please select a country");
			return false;
		}
		if(dto.getState().getId() <= 0){
			addActionMessage("Please select a state");
			return false;
		}
		return true;
	}

	public List<CountryDTO> getListCountry() {
		return listCountry;
	}

	public int getSelectedCountryId() {
		return selectedCountryId;
	}

	public List<StateDTO> getListState() {
		return listState;
	}
	@SkipValidation

	public void setDto(VendorDTO dto) {
		this.dto = dto;
	}

	@SkipValidation
	public void setListCountry(List<CountryDTO> listCountry) {
		this.listCountry = listCountry;
	}

	@SkipValidation
	public void setSelectedCountryId(int selectedCountryId) {
		this.selectedCountryId = selectedCountryId;
	}

	@SkipValidation
	public void setListState(List<StateDTO> listState) {
		this.listState = listState;
	}

	@Override
	public void prepare() throws Exception {
		listCountry = new UtilityService().getCountries();

		// selectedCountryId = dto.getState().getCountry().getId();

		// set selected state
		listState = UtilityService.getState(selectedCountryId);

		
	}
}
